// pages/classic/classic.js
import { ClassicModel } from '../../models/classic.js'
import { LikeModel } from '../../models/like.js'

const classicModel = new ClassicModel()
const likeModel = new LikeModel()

Component({
  /**
   * 页面的初始数据
   */

  properties: {
    cid: Number,
    type: Number
  },

  data: {
    classic: null,
    latest: true,
    first: false,
    likeCount: 0,
    likeStatus: false
  },

  lifetimes: {
    attached: function (options) {
      const { cid, type } = this.properties
      if(!cid) {
        classicModel.getLatest((res)=>{
          this.setData({
            classic: res,
            likeCount: res.fav_nums,
            likeStatus: res.like_status
          })
        })
      } else {
        classicModel.getById(cid, type, res => {
          this._getLikeStatus(res.id,res.type)
          this.setData({
            classic: res,
            latest: classicModel.isLatest(res.index),
            first: classicModel.isFirst(res.index)
          })
        })
      }
    }
  },

  methods: {
    onLike: function(evt) {
      const behavior = evt.detail.behavior
      const { id, type} = this.data.classic
      likeModel.like(behavior,id, type)
    },

    onPrev: function(evt) {
      this._updateClassic('previous')
    },

    onNext: function(evt) {
      this._updateClassic('next')
    },

    _updateClassic: function(nextOrPrevious) {
      const { index } = this.data.classic
      classicModel.getClassic(index, nextOrPrevious,(res)=>{
        const {id, type} = res;
        this._getLikeStatus(id, type)
        this.setData({
          classic: res,
          latest: classicModel.isLatest(res.index),
          first: classicModel.isFirst(res.index)
        })
      })
    },

    _getLikeStatus: function(artID, category) {
      likeModel.getClassicLikeStatus(artID, category, (res)=>{
        this.setData({
          likeCount: res.fav_nums,
          likeStatus: res.like_status
        })
      })
    }
  }
})

